package com.example.xuanyang.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;

/**
 * Created by xuanyang on 2017/10/26.
 */

public class AdActivity extends AppCompatActivity {


    TextView tv;
    com.google.android.gms.ads.InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);


        tv = (TextView) findViewById(R.id.myAd);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });




        mInterstitialAd = new com.google.android.gms.ads.InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());


//        mInterstitialAd.

        mInterstitialAd
                .setAdListener(new com.google.android.gms.ads.AdListener() {

                    @Override
                    public void onAdLoaded() {
                        // Code to be executed when an ad finishes loading.
                        Log.i("Ads", "onAdLoaded");
                        if(mInterstitialAd.isLoaded()){
                            Log.i("Ads", "show");
                            mInterstitialAd.show();
                        }
                    }

                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // Code to be executed when an ad request fails.
                        Log.i("Ads", "onAdFailedToLoad");
                    }

                    @Override
                    public void onAdOpened() {
                        // Code to be executed when the ad is displayed.
                        Log.i("Ads", "onAdOpened");
                    }

                    @Override
                    public void onAdLeftApplication() {
                        // Code to be executed when the user has left the app.
                        Log.i("Ads", "onAdLeftApplication");
                    }

                    @Override
                    public void onAdClosed() {
                        // Code to be executed when when the interstitial ad is
                        // closed.
                        Log.i("Ads", "onAdClosed");
                    }

                });


//        String NEW_FACEBOOK_ID_INTERSTITIAL = "1940482312840054_1966334506921501";
//        AdReplaceContext adContext = new AdReplaceContext(this, "com.gray.timer.phonecleanerpro");
//        final com.facebook.ads.InterstitialAd interstitialAd = new com.facebook.ads.InterstitialAd(adContext, NEW_FACEBOOK_ID_INTERSTITIAL);
//        if (interstitialAd != null) {
//            interstitialAd.setAdListener(new InterstitialAdListener() {
//                @Override
//                public void onInterstitialDisplayed(Ad ad) {
//                    // Interstitial displayed callback
//                }
//
//                @Override
//                public void onInterstitialDismissed(Ad ad) {
//                    // Interstitial dismissed callback
//                }
//
//                @Override
//                public void onError(Ad ad, AdError adError) {
//                    // Ad error callback
//                    Log.i("mytag_fb", "InterstitialAd error : "+ adError.getErrorMessage());
//                }
//
//                @Override
//                public void onAdLoaded(Ad ad) {
//                    Log.i("mytag_fb", "InterstitialAd success");
//                    if (interstitialAd.isAdLoaded()) {
//                        interstitialAd.show();
//                    }
//                }
//
//
//                @Override
//                public void onAdClicked(Ad ad) {
//                    // Ad clicked callback
//                }
//
//                @Override
//                public void onLoggingImpression(Ad ad) {
//                    // Ad impression logged callback
//                }
//            });
//        }
//
//        interstitialAd.loadAd();



    }





}
